#!/bin/bash

# Slackware build script for freeminer

# Copyright 2014-2015  Dimitris Zlatanidis  Orestiada, Greece
# I thank Larry Hajali for update to version 0.4.13.7
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 20220302 bkw: Modified by SlackBuilds.org: fix build on 15.0,
# by updating to latest git (80629f40a85fccafaefea1a490d9ca384f73493f).

# Note to maintainer: *please* don't use only a git hash as a version
# number. I used 0.4.14.8+20211109_80629f4, which includes the last
# tag before the commit, the date, and the commit hash. The reason
# for doing this is so we can tell at a glance which version is newer,
# when looking at several packages. Also notice the updated download
# URL in the .info file: it saves the file with the same name,
# regardless of what the user used to download it (whether or not it
# respects the Content-disposition header).

# I tested the game to the extent of trying to connect to a server,
# but one of the servers failed to connect, and the other connected,
# downloaded some content, then dropped the connection. Hope that's
# OK.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=freeminer
VERSION=${VERSION:-0.4.14.8+20211109_80629f4}
SRCVER=80629f40a85fccafaefea1a490d9ca384f73493f
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$SRCVER
unzip $CWD/$PRGNAM-$SRCVER.zip || unzip $CWD/$SRCVER.zip
cd $PRGNAM-$SRCVER
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \+ -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \+

# 20220302 bkw: our json.h is a system header.
find src -type f | \
  xargs grep -l '#include.*json/json\.h' | \
  xargs sed -i 's,"json/json.h",<json/json.h>,'

mkdir -p build
cd build
  cmake \
    -DJSON_INCLUDE_DIR=/usr/include/jsoncpp \
    -DRUN_IN_PLACE:BOOL=0 \
    -DENABLE_SYSTEM_JSONCPP:BOOL=ON \
    -DENABLE_SYSTEM_MSGPACK:BOOL=ON \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX:PATH=/usr \
    -DCUSTOM_BINDIR:PATH=games \
    -DCUSTOM_MANDIR:PATH=man \
    -DCUSTOM_LOCALEDIR:PATH=share/locale \
    -DCUSTOM_DOCDIR:PATH=doc/$PRGNAM-$VERSION \
    -DCUSTOM_EXAMPLE_CONF_DIR:PATH=doc/$PRGNAM-$VERSION \
    -DCMAKE_BUILD_TYPE=Release ..

  make
  make install/strip DESTDIR=$PKG
cd ..
gzip $PKG/usr/man/man*/*

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a LICENSE.txt *md $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
